package cn.com.dashihui.web.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.CommonKit;
import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.AdSeller;
import cn.com.dashihui.web.dao.Admin;
import cn.com.dashihui.web.dao.Seller;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.dao.User;
import cn.com.dashihui.web.dao.UserMoney;
import cn.com.dashihui.web.dao.UserValueLog;
import cn.com.dashihui.web.service.ChangeMoneyService;

@RequiresAuthentication
public class ChangeMoneyController extends BaseController{

	private ChangeMoneyService service = new ChangeMoneyService();

	public void index(){
		render("index.jsp");
	}

	/**
	 * 详情页面
	 */
	public void detail(){
		List<Store> storeList=service.findStoreList();
		int id = getCurrentUser().get("id");//获得登录用户id
		List<AdSeller> findAdSeller = service.findAdSeller();
		setAttr("findAdSeller", findAdSeller);
		List<Seller> sellerList = service.findAllSeller();
		setAttr("sellerList", sellerList);
		setAttr("storeList", storeList);
		setAttr("id", id);
		render("detail.jsp");
	}
	/**
	 * 全部会员信息
	 */
	public void page(){
		int pageNum = getParaToInt(0, 1);
		String phone=getPara("phone");//手机号
		String card=getPara("card");//会员卡号
		String sBeginDate=getPara("sBeginDate");//生日
		String sEndDate=getPara("sEndDate");
		String address=getPara("address");
		String sellerid=getPara("seller");//所属公司
		int toBossThreeId = 0 ;
		if (StrKit.notBlank(sellerid)) {
			//根据dao分公司名称查询营业部id
			Seller seller = service.findSellerById(sellerid);
			toBossThreeId = seller.get("id");
		}
		String storeid=getPara("store");//所属分公司
		int toBossTwoId = 0 ;
		if (StrKit.notBlank(storeid)) {
			//根据dao分公司名称查询营业部id
			Store store = service.findStoreById(storeid);
			toBossTwoId = store.get("id");
		}
		String dao=getPara("dao");
		int toBossOneId = 0 ;
		if (StrKit.notBlank(dao)) {
			//根据dao分公司名称查询营业部id
			AdSeller adSeller = service.findAdSellerByName(dao);
			toBossOneId = adSeller.get("id");
		} 
		String taici=getPara("taici");
		String state=getPara("state");

		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0,service.findByPage(pageNum, pageSize,phone,card,sBeginDate,sEndDate,address,toBossThreeId,toBossTwoId,toBossOneId,taici,state));
	}
	/**
     * 订单统计查询所需积分总和现金价格总和
     */
	public void findTotal(){
		int pageNum = getParaToInt(0, 1);
		String phone=getPara("phone");//手机号
		String card=getPara("card");//会员卡号
		String name=getPara("name");//会员卡号
		String sBeginDate=getPara("sBeginDate");//生日
		String sEndDate=getPara("sEndDate");
		String address=getPara("address");
		String sellerid=getPara("seller");//所属公司
		int toBossThreeId = 0 ;
		if (StrKit.notBlank(sellerid)) {
			//根据dao分公司名称查询营业部id
			Seller seller = service.findSellerById(sellerid);
			toBossThreeId = seller.get("id");
		}
		String storeid=getPara("store");//所属分公司
		int toBossTwoId = 0 ;
		if (StrKit.notBlank(storeid)) {
			//根据dao分公司名称查询营业部id
			Store store = service.findStoreById(storeid);
			toBossTwoId = store.get("id");
		}
		String dao=getPara("dao");
		int toBossOneId = 0 ;
		if (StrKit.notBlank(dao)) {
			//根据dao分公司名称查询营业部id
			AdSeller adSeller = service.findAdSellerByName(dao);
			toBossOneId = adSeller.get("id");
		} 
		String taici=getPara("taici");
		String state=getPara("state");

		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderSuccess(service.findTotal(pageNum, pageSize,phone,card,sBeginDate,sEndDate,address,toBossThreeId,toBossTwoId,toBossOneId,taici,state,name));
	}
	/**
	 * 通过转换审核
	 */
	public void doPass(){
		String id=getPara("id");
		String checktime=DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss");
		if(StrKit.isBlank(id)){
			renderFailed("参数id不能为空");
			return;
		}else{
			//获取转换对象
			int iid = Integer.parseInt(id);
			Record CtoM = service.findCtoM(iid);
			//获取转换人的信息
			int userid = CtoM.get("userId");
			User user = service.findUser(userid);
			String orderNo = CtoM.get("orderNo");
			Double oldAmount = user.get("money");
			//获取登录的用户
			Admin admin = getCurrentUser();
			String userName = admin.get("trueName");
			Double money = CtoM.get("money");
			System.out.println(money);
			int i= service.doPass(id,checktime,userName,money);
			System.out.println(i);
			if(i==2){
				double nowAmount = oldAmount + money;
				int type = 1;
				int orderType = 5;
				String describe = "余额转换";
				UserMoney userMoney = new UserMoney();
				Date createDate = new Date();
				userMoney.set("userid", userid)
					.set("amount", money)
					.set("fromOrderNum", orderNo)
					.set("type",type)
					.set("describe", describe)
					.set("createDate", createDate)
					.set("orderType", orderType)
					.set("oldAmount", oldAmount)
					.set("nowAmount", nowAmount)
					.save();
				renderSuccess(service.findById(Integer.valueOf(id)));
				return ;
			}else{
				//3.其他情况
				renderFailed("审核失败");
				return;
			}
		}
	}
	/**
	 * 驳回转换审核
	 */
	public void doReturn(){
		String id=getPara("id");
		String tmJypoint=getPara("tmJypoint");
		Double absJypoint = Math.abs(Double.valueOf(tmJypoint));
		String checktime=DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss");
		//获取返回前User的信息
		int userId = service.findUserId(id);
		User user = service.findUser(userId);
		//获取登录的用户
		Admin admin = getCurrentUser();
		String userName = admin.get("trueName");
		//在更新之前获取用户的四种积分值，作为日志里面的历史值
		double sppointHistory = user.getDouble("sppoint");
		double deduHistory = user.getDouble("jkpoint");
		double couponHistory = user.getDouble("jypoint");
		double ecouponHistory = user.getDouble("ecoupon");
		if(StrKit.isBlank(id)){
			renderFailed("参数id不能为空");
			return;
		}else{
			int i= service.doReturn(id,checktime,absJypoint,userName);
			if(i==2){
				UserValueLog userValueLog = new UserValueLog();
				int userid = user.getInt("id");
				int logType = 1;
				int flag = 13;
				int data = 1;
				String describe = "驳回";
				String orderNumStore = "BH"+DatetimeKit.getFormatDate("yyyyMMddHHmmssSSS")+CommonKit.randomNum(3);
				userValueLog.set("userid", userid).set("type", logType).set("flag", flag).set("fromOrderNum", orderNumStore)
					.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss")).set("data", data).set("describe", describe)
					.set("sppointHistory", sppointHistory).set("deduHistory", deduHistory).set("couponHistory", couponHistory)
					.set("ecouponHistory", ecouponHistory);
				userValueLog.set("coupon", absJypoint);
				userValueLog.save();
				renderSuccess(service.findById(Integer.valueOf(id)));
				return ;
			}else{
				//3.其他情况
				renderFailed("审核失败");
				return;
			}
		}
	}
	/*登录验证*/
	//登录
	/**
	 * "account", "6874"
	 * "pwd", "a5555555"
	 * 登录
	 * @return -1：异常，0：成功，1：用户名为空，2：用户名格式不正确，
	 */
	public void login(){
		//设置账号
		//获取前端录入的密码
		String passWord = getPara("passWord");
		boolean b = passWord.equals("111111");
		//请求api
		if(b == true){
			//登录成功
			renderSuccess();
		}else{
			//登录失败
			renderResult(2);
		}
	}
}